#utf-8

import os
import shutil

def replace_in_file(file_path, old_string, new_string):
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            content = file.read()
        
        # 替换内容
        new_content = content.replace(old_string, new_string)
        
        with open(file_path, 'w', encoding='utf-8') as file:
            file.write(new_content)
        
        print(f"已处理文件: {file_path}")
    except Exception as e:
        print(f"处理文件 {file_path} 时出错: {e}")

def traverse_and_replace(directory, old_string, new_string, skip_dirs=None):
    """
    遍历指定目录下的所有文件，替换指定字符串内容，跳过指定的目录
    :param directory: 要遍历的目录路径
    :param old_string: 要替换的旧字符串
    :param new_string: 替换后的新字符串
    :param skip_dirs: 需要跳过的目录列表，默认为空
    """
    if skip_dirs is None:
        skip_dirs = []

    for root, dirs, files in os.walk(directory):
        # 检查当前目录是否在跳过列表中
        if os.path.basename(root) in skip_dirs:
            print(f"跳过目录: {root}")
            dirs.clear()  # 清空子目录列表，跳过当前目录的搜索
            continue

        for file in files:
            file_path = os.path.join(root, file)
            replace_in_file(file_path, old_string, new_string)
            
            
            

def find_and_replace_file(directory, target_file, search_filename, skip_dirs=None):
    """
    遍历指定目录下的所有文件，找到名为 search_filename 的文件并替换为 target_file，跳过指定的目录
    :param directory: 要遍历的目录路径
    :param target_file: 用于替换的文件路径
    :param search_filename: 要查找并替换的文件名
    :param skip_dirs: 需要跳过的目录列表，默认为空
    """
    # 确保目标文件存在
    if not os.path.exists(target_file):
        print(f"目标文件 {target_file} 不存在。")
        return

    # 如果跳过目录列表为空，则初始化为空列表
    if skip_dirs is None:
        skip_dirs = []

    for root, dirs, files in os.walk(directory):
        # 检查当前目录是否在跳过列表中
        if os.path.basename(root) in skip_dirs:
            print(f"跳过目录: {root}")
            dirs.clear()  # 清空子目录列表，跳过当前目录的搜索
            continue

        for file in files:
            if file == search_filename:
                file_path = os.path.join(root, file)
                try:
                    # 备份原始文件
                    backup_path = file_path + ".bak"
                    shutil.copy(file_path, backup_path)
                    print(f"已备份原始文件: {file_path} -> {backup_path}")

                    # 替换文件
                    shutil.copy(target_file, file_path)
                    print(f"已替换文件: {file_path} 为 {target_file}")
                except Exception as e:
                    print(f"处理文件 {file_path} 时出错: {e}")
                    
if __name__ == "__main__":
    # 设置目录路径和要替换的字符串
    directory = "../"  # 替换为你要遍历的目录路径
    skip_dirs = ["oh_modules", ".idea", ".idea",".hvigor",".git"]  # 跳过的目录

    #old_string = "曲韵风华"
    #new_string = "水木年华"

    #old_id = "6917559436623785967"
    #new_id = "112233445566778899"
    
    #traverse_and_replace(directory, old_string, new_string,skip_dirs)

    #traverse_and_replace(directory, old_id, new_id,skip_dirs)
    
    target_file = "./app_icon.png"  # 替换为你要使用的目标文件路径
   
   
    search_filename = "icon.png"  # 替换为你要查找的文件名
    find_and_replace_file(directory, target_file, search_filename,skip_dirs)

    search_filename = "startIcon.png"  # 替换为你要查找的文件名
    find_and_replace_file(directory, target_file, search_filename,skip_dirs)
